﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VTLive.Models
{
    public class AssociateRepository
    {
        VTliveDataContext db = new VTliveDataContext();

        public IQueryable<Associate> GetAllAssociates()
        {
            return db.Associates;
        }

        public Associate GetAssociate(int AssID)
        {
            return db.Associates.SingleOrDefault(s => s.ID == AssID);
        }

        public Associate GetAssociateByUserIDAndStationId(int UID, int SID)
        {
            return (from ass in db.Associates 
                    where ass.StationID == SID && ass.UserID == UID 
                    select ass).SingleOrDefault();
        }

        public void Add(Associate MyAss)
        {
            db.Associates.InsertOnSubmit(MyAss);
        }

        public IQueryable<Associate> GetAllAssociatesByStation(int stationID)
        {
            var StationAssociates = (from associate in db.Associates where associate.StationID == stationID select associate);
            return StationAssociates;
        }
        public void Delete(Associate MyAss)
        {
            db.Associates.DeleteOnSubmit(MyAss);

            var AssSuggestions = from suggestion in db.Suggestions where suggestion.AssociateID == MyAss.ID select suggestion;
            db.Suggestions.DeleteAllOnSubmit(AssSuggestions);
        }

        public void Save()
        {
            db.SubmitChanges();
        }

        internal bool IsAssociate(int UserId, Station s)
        {
            var ass = (from a in db.Associates where a.UserID == UserId select a);
            if (ass.Count() != 0) return true;
            else return false;
        }
    }
}
